// ListView.cs
//

using System;
using System.Collections.Generic;
using jQueryApi;

namespace TuringMachine
{
   public delegate void ItemEventHandler(Div sender,EventArgs ev);
   public class ListView : Div
   {
      int m_iDefaultHeight;
      Div m_selectedItem;
      public event ItemEventHandler OnItemClicked;

      public ListView(jQueryObject parent, string id) :
         base(parent, id)
      {
         pObject.CSS("background-color", "White");
         pObject.CSS("border", "1px solid black");
         pObject.CSS("overflow", "scroll");
         m_iDefaultHeight = 60;
         m_selectedItem = null;
      }

      public void AddItem(Div item)
      {
         item.pObject.CSS("margin", "2px");
         item.pObject.CSS("padding", "2px 20px 2px 20px");
         item.pObject.CSS("position", "static");
         item.pObject.CSS("height", m_iDefaultHeight + "px");
         item.pObject.CSS("border", "1px solid black");
         item.pObject.CSS("overflow", "hidden");
         item.pObject.CSS("width", (pObject.GetWidth()-70) + "px");
         item.pObject.Click(delegate
         {
            SelectItem(item);
         });
      }

      public void SelectItem(Div item)
      {
         if (m_selectedItem != null)
            m_selectedItem.pObject.CSS("background-color", "White");
         item.pObject.CSS("background-color", "Yellow");
         m_selectedItem = item;
         if (OnItemClicked != null)
            OnItemClicked(m_selectedItem, null);
      }

      public int pDefaultHeight
      {
         get { return m_iDefaultHeight; }
         set { m_iDefaultHeight = value; }
      }
   }
}
